/**
 * Created by chenyong on 2015/7/28.
 */

var krpanoplugin = function(){
    var local = this;
    var krpano = null;
    var plugin = null;

    var plugincanvascontext = null;

    local.registerplugin = function(krpanointerface, pluginpath, pluginobject)
    {
        krpano = krpanointerface;
        plugin = pluginobject;
        initplugin();
    };

    local.hittest = function(x,y)
    {
        if (plugincanvascontext)
        {
            return plugincanvascontext.isPointInPath(x,y);
        }
        return false;
    };

    local.onresize = function(width,height)
    {
        return false;
    };

    /**
     * 隐藏所有hotspot
     */
    function hidetags(){

    }

    /**
     * 显示所有hotspot
     */
    function showtags(){

    }

    var objTagStageValues = new Object();

    function initplugin(){
        var no_errors = true;
        if(plugin.tagpointsurl == undefined){
            no_errors = false;
        }
    };

    function get_tagsdata(onloadedaction)
    {

    }

    //计算全景图的可视区域
    function calc_visible_stage()
    {
        objTagStageValues.width = 0;
        objTagStageValues.width_middle = 0;
        objTagStageValues.height = 0;
        objTagStageValues.height_middle = 0;
        objTagStageValues.atv_min = 0;
        objTagStageValues.ath_min = 0;
        objTagStageValues.atv_max = 0;
        objTagStageValues.ath_max = 0;
        objTagStageValues.fov = 0;

        objTagStageValues.fov = krpano.view.fov;

        objTagStageValues.width = krpano.get("stagewidth");
        objTagStageValues.width_middle = parseInt(objTagStageValues.width) / 2;
        krpano.set("width_middle", objTagStageValues.width_middle);

        objTagStageValues.height = krpano.get("stageheight");
        objTagStageValues.height_middle = parseInt(objTagStageValues.height) / 2;
        krpano.set("height_middle", objTagStageValues.height_middle);

        krpano.call(String("screentosphere(0,height_middle,fc_ath_min,dummy);"));
        objTagStageValues.ath_min = krpano.get('fc_ath_min');

        krpano.call(String("screentosphere(stagewidth,height_middle,fc_ath_max,dummy);"));
        objTagStageValues.ath_max = krpano.get("fc_ath_max");

        krpano.call(String("screentosphere(width_middle,0,dummy,fc_atv_min);"));
        objTagStageValues.atv_min = krpano.get('fc_atv_min');

        krpano.call(String("screentosphere(width_middle,stageheight,dummy,fc_atv_max);"));
        objTagStageValues.atv_max = krpano.get("fc_atv_max");

        fc_hlookat = signed_h(krpano.view.hlookat);

        // calculation bug in screentosphere krpano
        if (objTagStageValues.ath_min < objTagStageValues.ath_max)
        {
            if (objTagStageValues.ath_min>fc_hlookat){
                objTagStageValues.ath_min = signed_h(fc_hlookat - krpano.view.fov/2);
            }
            if (objTagStageValues.ath_max<fc_hlookat){
                objTagStageValues.ath_max = signed_h(krpano.view.hlookat + krpano.view.fov/2);
            }
        }
        else
        {
            if (objTagStageValues.ath_min<fc_hlookat){
                objTagStageValues.ath_min = signed_h(fc_hlookat - krpano.view.fov/2);
            }
            if (objTagStageValues.ath_max>fc_hlookat){
                objTagStageValues.ath_max = signed_h(krpano.view.hlookat + krpano.view.fov/2);
            }
        }
        /* -90 <=> 90 */
        if (objTagStageValues.atv_min>krpano.view.vlookat){
            console.log("atv_min > vlookat, atv_min="+objTagStageValues.atv_min+ " vlookat="+ krpano.view.vlookat +" defaulting to fov/2="+ String(krpano.view.vlookat - krpano.view.fov/2));
            objTagStageValues.atv_min = krpano.view.vlookat - krpano.view.fov/2;
        }
        if (objTagStageValues.atv_max<krpano.view.vlookat){
            objTagStageValues.atv_max = krpano.view.vlookat + krpano.view.fov/2;
        }
    }

    function signed_h(ath){
        h = ath%360;
        if (h<=-180){
            h = 360+h;
        }
        else if(h>180){
            h = 360-h;
        }
        return h;
    }
};
